package com.ponxu.mb.web.action.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ponxu.log.Log;
import com.ponxu.log.LogFactory;
import com.ponxu.mb.model.PageInfo;
import com.ponxu.mb.model.Post;
import com.ponxu.mb.service.CategoryService;
import com.ponxu.mb.service.PostService;
import com.ponxu.mb.web.action.MiniBlogAction;
import com.ponxu.utils.Lang;

/**
 * 后台首页, 查出文章列表
 * 
 * @author xwz
 */
public class IndexAction extends MiniBlogAction {
	private static final Log LOG = LogFactory.getLog();
	private static final String FTL_INDEX = "/admin/index.ftl";

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LOG.debug("admin index...");
		PageInfo page = new PageInfo(request);
		String keywords = request.getParameter("keywords");
		List<Post> list = null;

		if (Lang.isNotEmpty(keywords)) {
			list = PostService.searchAllPostsByKeywords(keywords, page);
		} else {
			list = PostService.searchAllPosts(page);
		}

		CategoryService.populateForPosts(list);
		
		request.setAttribute("page", page);
		request.setAttribute("list", list);
		request.setAttribute("keywords", keywords);
		request.setAttribute("status", PostService.POST_STATUS);

		return FTL_INDEX;
	}
}